var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

var pageNo = 1;
var hadLastPage = false;
var isLoad = 0;

Page({
  data: {
    couponList: []
  },
  onShow: function (options) {
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      recordList: []
    });
    this.loadList();
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    this.loadList();
  },
  //获取优惠券列表
  loadList: function () {
    isLoad = 1;
    if (hadLastPage) {
      // wx.showToast({
      //   title: '到底了！',
      // });
      isLoad = 0;
      return;
    }
    var that = this;
    var data = {
      pageNo: pageNo
    }
    util.request(api.CouponList, data, 'POST').then(function (data) {
      if (data.type == 'success') {
        var couponList = that.data.couponList;

        for (var i = 0; i < data.data.couponList.length; i++) {
          couponList.push(data.data.couponList[i]);
        }

        if (data.data.pageNo == data.data.totalPages) {
          hadLastPage = true;
        } else {
          pageNo++;
        }

        that.setData({
          couponList: couponList
        });
        isLoad = 0;
      } else {
        console.log(data.content);
        isLoad = 0;
      }
    });
  }
})